# coding=utf-8

import sys
import python_library.utils as utils
from bind_common import MySQLConn
from bind_common import BindConfig
reload(sys)
sys.setdefaultencoding("utf-8")


class ManageVideoLoad(object):
  """管理加载到内存中的video
  """

  def _fetch_video_list(self, video_id_set):
    """得到传入video id字段信息

    Args:
      video_id_set: video信息，仅会有一个值

    Returns:
      video列表，长度最大为1
    """


  @staticmethod
  def get_video_from_database(video_id):
    """从数据库中查出传入video_id的video信息

    Args:
      video_id: 待操作字符串

    Returns:
      传入video_id的video信息
    """
    with MySQLConn(BindConfig.DB) as db:
      conn = db.get()
      sql = "SELECT %s FROM video WHERE id=%s" % (BindConfig.VIDEO_FIELDS, video_id)
      dataset = utils.run_sql(conn, sql)
      if not dataset:
        return None
      fields = [BindConfig.VIDEO_ALBUM_MAP[field]
                if field in BindConfig.VIDEO_ALBUM_MAP else field
                for field in BindConfig.VIDEO_FIELDS.split(",") if field]
      video_list = map(lambda x: {k: v for k, v in zip(fields, x)}, dataset)
      if not video_list:
        return None
      return video_list[0]